package com.financialDog.javaTool.services.accountManage;


import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.financialDog.javaTool.common.PropertyConstant;
import com.financialDog.javaTool.controller.accountManage.AccountManageController;
import com.financialDog.javaTool.controller.fileManage.FileManageController;
import com.financialDog.javaTool.reportModel.KeyValue;
import com.xwintop.xcore.util.javafx.TooltipUtil;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
@Slf4j
public class AccountManageService {

    private AccountManageController accountManageController;

    private JSONObject accountObject;

    private String currentKey = StringUtils.EMPTY;

    public AccountManageService(AccountManageController accountManageController) {
        this.accountManageController = accountManageController;
    }

    public TreeItem<KeyValue> getRootAccountTree(){
        String str = ResourceUtil.readUtf8Str("config/calculate_account.json");
        JSONObject accountObject = JSONObject.parseObject(str);
        TreeItem<KeyValue> rootItem = new TreeItem<KeyValue>(new KeyValue("account","指标"));
        rootItem.setExpanded(true);
        for(String key : accountObject.keySet()){
            JSONObject keyObject = accountObject.getJSONObject(key);
            String name = keyObject.getString(PropertyConstant.NAME_KEY);
            TreeItem<KeyValue> treeItem = new TreeItem<KeyValue>(new KeyValue(key, name));
            rootItem.getChildren().add(treeItem);
        }

        return rootItem;
    }

    public void setTableData(ObservableList<Map<String, String>> tableData, String key){

        if (!accountObject.containsKey(key)){
            TooltipUtil.showToast("指标公式获取异常");
        }
        this.currentKey = key;

        JSONObject keyObject = accountObject.getJSONObject(key);
        JSONArray formulaList = keyObject.getJSONArray(PropertyConstant.FORMULA_KEY);
        for(int i = 0 ; i < formulaList.size(); i++){
            JSONObject formulaObject = formulaList.getJSONObject(i);
            Boolean isDefault = formulaObject.getBooleanValue("isDefault");
            String formulaLine = formulaObject.getString("line");
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("formulaTableColumn", formulaLine);
            hashMap.put("enableTableColumn", isDefault.toString());
            tableData.add(hashMap);
        }
    }

    public void setDefaultLine(ObservableList<Map<String, String>> tableData, Integer index){
        for (int i = 0 ;  i < tableData.size(); i++){
            Map<String, String> dataRow = tableData.get(i);
            String enableStr = dataRow.get("enableTableColumn");
            if (!accountObject.containsKey(this.currentKey)){
                TooltipUtil.showToast("指标公式获取异常");
            }
            JSONObject selectObject = accountObject.getJSONObject(this.currentKey);
            JSONObject formulaObject = selectObject.getJSONArray(PropertyConstant.FORMULA_KEY).getJSONObject(i);
            if (i != index){
                if (enableStr.equals("true")){
                    dataRow.put("enableTableColumn", "false");
                    formulaObject.put(PropertyConstant.IS_DEFAULT_KEY, false);
                }
            }else{
                formulaObject.put(PropertyConstant.IS_DEFAULT_KEY, true);
            }
        }
    }

    public String getSelectFormula(String key){
        if (!accountObject.containsKey(key)){
            TooltipUtil.showToast("指标公式获取异常");
        }
        JSONObject keyObject = accountObject.getJSONObject(key);
        JSONArray jsonArray = keyObject.getJSONArray(PropertyConstant.FORMULA_KEY);
        for (int i = 0; i < jsonArray.size(); i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Boolean isdDefault = jsonObject.getBooleanValue("isDefault");
            if (isdDefault){
                return jsonObject.getString(PropertyConstant.LINE_KEY);
            }
        }
        return StringUtils.EMPTY;
    }

    public void setCacheJsonObject(JSONObject accountObject){
        this.accountObject = accountObject;
    }

}
